<template>
  <div class="number-pad">
    <div class="output">{{ value || '0' }}</div>
    <div class="buttons clearfix">
      <button @click="inputContent">1</button>
      <button @click="inputContent">2</button>
      <button @click="inputContent">3</button>
      <button @click="remove">删除</button>
      <button @click="inputContent">4</button>
      <button @click="inputContent">5</button>
      <button @click="inputContent">6</button>
      <button @click="clear">清空</button>
      <button @click="inputContent">7</button>
      <button @click="inputContent">8</button>
      <button @click="inputContent">9</button>
      <button @click="ok" class="ok">OK</button>
      <button @click="inputContent" class="zero">0</button>
      <button @click="inputContent">.</button>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import {Component, Prop} from 'vue-property-decorator';

@Component
export default class NumberPad extends Vue {
  @Prop() value!: string;

  inputContent(event: MouseEvent) {
    //强制event.target指定类型
    const button = (event.target as HTMLButtonElement);
    const input = button.textContent as string; //input的值不会为空
    this.$emit('update:inputContent', input);
  }

  remove() {
    this.$emit('update:value', this.value.slice(0, -1));
  }

  clear() {
    this.$emit('update:value', '');
  }

  ok() {
    if (!this.value) return;
    this.$emit('update:submit', this.value);
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/style/helper.scss";

.number-pad {
  > .output {
    @extend %innerShadow;
    font-size: 36px;
    font-family: Consolas, monospace;
    padding: 9px 16px;
    text-align: right;
  }

  > .buttons {
    @extend %clearfix;

    > button {
      width: 25%;
      height: 64px;
      float: left;
      background-color: transparent;
      border: none;
      border-radius: unset;

      &.ok {
        float: right;
        height: 64px*2;
      }

      &.zero {
        width: 50%;
      }

      $bg: #f2f2f2;

      &:nth-child(1) {
        background-color: $bg;
      }

      &:nth-child(2), &:nth-child(5) {
        background-color: darken($bg, 4%);
      }

      &:nth-child(3), &:nth-child(6), &:nth-child(9) {
        background-color: darken($bg, 4*2%);
      }

      &:nth-child(4), &:nth-child(7), &:nth-child(10) {
        background-color: darken($bg, 4*3%);
      }

      &:nth-child(8), &:nth-child(11), &:nth-child(13) {
        background-color: darken($bg, 4*4%);
      }

      &:nth-child(12) {
        background-color: darken($bg, 4%*6);
      }

      &:nth-child(14) {
        background-color: darken($bg, 4%*5);
      }
    }
  }
}
</style>
